package com.example.administrator.exkotlin.ui.list

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.example.administrator.exkotlin.R
import kotlinx.android.synthetic.main.activity_chat.*
import java.lang.IllegalArgumentException

/**
 * 聊天界面练习
 * */
class ChatActivity : AppCompatActivity(), View.OnClickListener {

    var msgList = ArrayList<Msg>()
    val mChatAdapter = ChatAdapter(msgList)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_chat)
        iniData()
    }

    private fun iniData() {
        btn_receive.setOnClickListener(this)
        btn_send.setOnClickListener(this)
        rv_chat.layoutManager = androidx.recyclerview.widget.LinearLayoutManager(this)
        rv_chat.adapter = mChatAdapter

    }

    override fun onClick(v: View?) {
        val msg = Msg()
        msg.msg = ed_input.text.toString()
        when (v?.id) {
            R.id.btn_receive -> {
                if (!TextUtils.isEmpty(msg.msg)) {
                    msg.msgType = Msg.TYPE_RECIVE
                    notifyDatas(msg)
                }
            }
            R.id.btn_send -> {
                if (!TextUtils.isEmpty(msg.msg)) {
                    msg.msgType = Msg.TYPE_SEND
                    notifyDatas(msg)
                }
            }
        }
        ed_input.setText("")
    }

    fun notifyDatas(msg: Msg) {
        msgList.add(msg)
        mChatAdapter.notifyItemInserted(msgList.size - 1)
        rv_chat.scrollToPosition(msgList.size - 1)
    }

    class ChatAdapter(var msgList: List<Msg>) : androidx.recyclerview.widget.RecyclerView.Adapter<androidx.recyclerview.widget.RecyclerView.ViewHolder>() {
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): androidx.recyclerview.widget.RecyclerView.ViewHolder = if (viewType == Msg.TYPE_SEND) {
            val view = LayoutInflater.from(parent?.context).inflate(R.layout.chat_send_item, parent, false)
            sendViewHolder(view)
        } else {
            val view = LayoutInflater.from(parent?.context).inflate(R.layout.chat_recevie_item, parent, false)
            recevieViewHolder(view)
        }

        override fun getItemCount(): Int {
            return msgList.size
        }

        override fun onBindViewHolder(holder: androidx.recyclerview.widget.RecyclerView.ViewHolder, position: Int) {
            val msg = msgList[position]
            when (holder) {
                is sendViewHolder -> {
                    holder.item_view?.text = msg.msg
                }
                is recevieViewHolder -> {
                    holder.item_view?.text = msg.msg
                }
                else -> {
                    throw IllegalArgumentException()
                }
            }
        }

        override fun getItemViewType(position: Int): Int {
            return msgList.get(position).msgType
        }

        inner class sendViewHolder(itemView: View) : androidx.recyclerview.widget.RecyclerView.ViewHolder(itemView) {
            val item_view = itemView?.findViewById<TextView>(R.id.tv_send_item)
        }

        inner class recevieViewHolder(itemView: View) : androidx.recyclerview.widget.RecyclerView.ViewHolder(itemView) {
            val item_view = itemView?.findViewById<TextView>(R.id.tv_reveive_item)
        }

    }

    class Msg(var msg: String = "", var msgType: Int = TYPE_SEND) {
        companion object {
            const val TYPE_SEND = 0
            const val TYPE_RECIVE = 1
        }
    }
}

